package cn.wzl.recognition.bpnn.util;

public class PictureRecognitionConverter {

    public static int convertOutputArrayToNumber(double[] output, boolean printable) {
        int maxIndex = 1;
        boolean isRecognized = false;
        for(int i = 1; i < output.length; i ++) {
            if(printable) {
                System.out.println("number='" + (i - 1) + "',rate=" + (int)(output[i]*100) + "%");
            }
            if(output[i] > output[maxIndex]) {
                maxIndex = i;
            }
            if(output[i] > 0.1) {
                isRecognized = true;
            }
        }
        if(isRecognized) {
            return maxIndex - 1;
        }
        else{
            return -1;
        }
    }

    public static double[] convertNumberToOutputArray(int index) {
        double[] output = new double[10];
        if(index < 10) {
            output[index] = 1;
        }
        return output;
    }

    public static double[][] formatLabels(double[][] data) {
        double[][] ret = new double[data.length][];
        for(int i = 0; i < data.length; i ++) {
            ret[i] = PictureRecognitionConverter.convertNumberToOutputArray((int) data[i][0]);
        }
        return ret;
    }

    public static double[][] formatData(double[][] data) {
        double[][] ret = new double[data.length][data[0].length];
        for(int i = 0; i < data.length; i ++) {
            for(int j = 0;j < data[i].length; j ++) {
                ret[i][j] = data[i][j] > 127 ? 1 : 0;
            }
        }
        return ret;
    }
}